package com.tianzeculture.stagebao.activity.personal;

import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.tianzeculture.stagebao.R;
import com.tianzeculture.stagebao.activity.login.RegisterActivity;
import com.tianzeculture.stagebao.base.BaseActivity;
import com.tianzeculture.stagebao.bean.ResultBean;
import com.tianzeculture.stagebao.net.UserRestUsage;
import com.tianzeculture.stagebao.network.Request;
import com.tianzeculture.stagebao.network.enity.MSG;
import com.tianzeculture.stagebao.network.util.ToastUtil;
import com.tianzeculture.stagebao.util.StatusBarUtil;
import com.tianzeculture.stagebao.util.Util;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/6/25 0025.
 * 修改手机号
 */

public class ChangePhoneActivity extends BaseActivity {
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.edtPhone)
    EditText edtPhone;
    @BindView(R.id.edtCode)
    EditText edtCode;
    @BindView(R.id.tvGetCode)
    TextView tvGetCode;
    @BindView(R.id.btnSure)
    Button btnSure;

    @Request
    UserRestUsage userRestUsage = null;

    private int GET_CODE = 101;//获取验证码
    private int UPDATE_PHONE = 102;//更换手机号
    private String phone = "";//获取验证码的手机号
    private String code = "";//验证码


    @Override
    public void initUI() {
        super.initUI();
        StatusBarUtil.setWindowStatusBarColor(this, R.color.colorGray_F8);
        tvTitle.setText("修改手机号");

        time = new TimeCount(10000, 1000);
    }

    TimeCount time;

    @OnClick(R.id.tvGetCode)
    public void getCode(View view) {
        if (Util.isPhone(edtPhone.getText().toString().trim())) {//1注册 2重置密码 1更换密码
            phone = edtPhone.getText().toString().trim();//保存获取验证码的手机号
            userRestUsage.getCode(GET_CODE, edtPhone.getText().toString().trim(), 1);
            time.start();
        }
    }

    @OnClick(R.id.btnSure)
    public void updatePhone() {
        if (code.equals("")) {
            ToastUtil.showShort(this, "请先获取验证码!");
            return;
        } else if (!edtCode.getText().toString().trim().equals(code)) {
            ToastUtil.showShort(this, "验证码错误!");
            return;
        } else if (!phone.equals(edtPhone.getText().toString().trim())) {
            ToastUtil.showShort(this, "该手机号码与获取验证码手机不匹配!");
            return;
        }
        userRestUsage.updateIphoneNum(UPDATE_PHONE, phone,
                edtCode.getText().toString().trim());
    }

    @Override
    public void refreshUI(int taskId, MSG msg) {
        super.refreshUI(taskId, msg);
        if (taskId == GET_CODE) {
            if (msg.getObj() != null) {
                ResultBean resultBean = (ResultBean) msg.getObj();
                code = resultBean.getCode();
            }
        }
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_change_phone;
    }


    class TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
//            tvGetCode.setBackgroundColor(getColor(R.color.colorGray_D7));
            tvGetCode.setClickable(false);
//            tvGetCode.setText("(" + millisUntilFinished / 1000 + ") 秒后可重新发送");
            tvGetCode.setText(millisUntilFinished / 1000 + "s");
        }

        @Override
        public void onFinish() {
            tvGetCode.setText("重新获取验证码");
            tvGetCode.setClickable(true);
//            tvGetCode.setBackgroundColor(getColor(R.color.colorGray_777));
        }
    }
}
